/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Manutençã.java
 *
 * Created on 27/06/2009, 10:24:33
 */

package src.view;

import src.control.UsuarioBO;
import src.control.UsuarioLogado;
import src.dto.UsuarioDTO;
/**
 *
 * @author camila
 */
public class ManutençãoUsuario extends javax.swing.JFrame {

    /** Creates new form Manutençã */
    public ManutençãoUsuario() {

        initComponents();
        if (UsuarioLogado.getInstance().getId() != 0){
            login.setText(UsuarioLogado.getInstance().getLogin());
            nome.setText(UsuarioLogado.getInstance().getNome());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        confirmacaoSenha = new javax.swing.JPasswordField();
        senha = new javax.swing.JPasswordField();
        login = new javax.swing.JTextField();
        nome = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        aviso = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setAlwaysOnTop(true);
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 1, 14));
        jLabel1.setText("Manutenção de Usuário");
        jLabel1.setName("jLabel1"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel2.setText("Nome:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel3.setText("Login:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel4.setText("Senha:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 12));
        jLabel5.setText("Confirmar Senha:");
        jLabel5.setName("jLabel5"); // NOI18N

        confirmacaoSenha.setName("confirmacaoSenha"); // NOI18N

        senha.setName("senha"); // NOI18N

        login.setName("login"); // NOI18N

        nome.setName("nome"); // NOI18N

        jButton1.setText("Gravar");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Sair");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        aviso.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        aviso.setFont(new java.awt.Font("Verdana", 1, 10));
        aviso.setForeground(new java.awt.Color(255, 0, 0));
        aviso.setBorder(null);
        aviso.setName("aviso"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(aviso, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                        .addGap(50, 50, 50)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nome, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                            .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(confirmacaoSenha, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(senha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 462, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(confirmacaoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(aviso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2)))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();	//Fecha tela de login
        if(UsuarioLogado.getInstance().getId() != 0)
            new RelacaoTarefas().setVisible(true);//Abre o tela principal
        else
            new Login().setVisible(true);//Abre o tela principal
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        UsuarioDTO usuario = new UsuarioDTO();
        UsuarioBO execute = new UsuarioBO();
        int codigoResp;
        int comp = String.valueOf(senha.getPassword()).compareTo(String.valueOf(confirmacaoSenha.getPassword()));

        if (UsuarioLogado.getInstance().getId() != 0){
            if(!String.valueOf(senha.getPassword()).isEmpty()){
                if(comp == 0){
                    usuario.setId_usuario(UsuarioLogado.getInstance().getId());
                    usuario.setLogin(login.getText());
                    usuario.setNome(nome.getText());
                    usuario.setSenha(String.valueOf(senha.getPassword()));
                    aviso.setText(execute.alterarDados(usuario));
                    senha.setText("");
                    confirmacaoSenha.setText("");

                }else{
                    usuario.setId_usuario(UsuarioLogado.getInstance().getId());
                    usuario.setLogin(login.getText());
                    usuario.setNome(nome.getText());
                    usuario.setSenha(UsuarioLogado.getInstance().getSenha());
                    aviso.setText(execute.alterarDados(usuario));
                }
            }
        }else{
            //Buscando valor das senhas para converter em string, preenchendo
            // Usuario DTO
            String confSenha = String.valueOf(confirmacaoSenha.getPassword());
            usuario.setNome(nome.getText());
            usuario.setLogin(login.getText());
            usuario.setSenha(String.valueOf(senha.getPassword()));

            /*Verificando se dados preenchido corretamente.
             * Código Erro:
             * 0 - Ok
             * 1 - Senha não confere
             * 2 - Nem todos os dados foram preenchidos.
             * 3 - Login já cadastrado.
             */
            codigoResp = execute.testandoPreenchimento(usuario, confSenha);
            switch (codigoResp){
                case 0:
                    if(execute.cadastraUsuario(usuario)){
                        nome.setText("");
                        login.setText("");
                        senha.setText("");
                        confirmacaoSenha.setText("");
                        aviso.setText("Dados cadastrados corretamente!");
                    }else
                        aviso.setText("Houve falha no cadastro!");
                    break;
                case 1:
                    aviso.setText("A senha não confere");
                    break;
                case 2:
                    aviso.setText("O preenchimento de todos os campos é obrigatório!");
                    break;
                case 3:
                    aviso.setText("Login já cadastrado!");
                    break;
            }
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.dispose();	//Fecha tela de login
        new Login().setVisible(true);//Abre o tela principal
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ManutençãoUsuario().setVisible(true);
    }
    });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aviso;
    private javax.swing.JPasswordField confirmacaoSenha;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField login;
    private javax.swing.JTextField nome;
    private javax.swing.JPasswordField senha;
    // End of variables declaration//GEN-END:variables

}
